/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"net"

	"gitee.com/chuanjinge/go-container/container"
	"gitee.com/chuanjinge/go-container/container/vethbr"
	"github.com/spf13/cobra"
)

var (
	createDriver string
	createSubnet string
)

var createCmd = &cobra.Command{
	Use:   "create <NETWORK_NAME> --driver=<DRIVER --subnet=<IP/MASK>",
	Short: "xxx network create <NETWORK_NAME> --driver=bridge --subnet=192.168.34.0/24",
	Long:  `根据指定参数创建网络，如果网络名称已经存在则报错，如果子网段已经被占用则报错`,
	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) < 1 {
			return fmt.Errorf("请在参数中指定网络名称")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("Creating network...")
		// 检查子网是否被占用了
		if createSubnet != "" {
			_, subnet, err := net.ParseCIDR(createSubnet)
			if err != nil {
				fmt.Println(err.Error())
				return
			}

			if err := vethbr.IsSubnetInUse(subnet); err != nil {
				fmt.Println(err.Error())
				return
			}
		}
		// 在这里添加您的逻辑
		if createDriver == "bridge" {
			if err := container.CreateBridgeNetwork(args[0], createSubnet); err != nil {
				fmt.Println(err.Error())
				return
			}
			fmt.Printf("bridge %s create success\n", args[0])
		} else {
			fmt.Printf("暂不支持%s类型的网络驱动", createDriver)
		}
	},
}

var listCmd = &cobra.Command{
	Use:   "list",
	Short: "list a new network",
	Long:  `list a new network with specified parameters.`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("List network...")
		// 在这里添加您的逻辑
		if err := container.ListNetwork(); err != nil {
			fmt.Println(err.Error())
			return
		}
	},
}

var removeCmd = &cobra.Command{
	Use:   "remove",
	Short: "remove <NET_NAME>",
	Long:  `remove <NET_NAME>`,
	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) < 1 {
			return fmt.Errorf("请在参数中指定网络名称")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("Remove network...")
		// 在这里添加您的逻辑
		if err := container.RemoveNetwork(args[0]); err != nil {
			fmt.Println(err.Error())
			return
		}
	},
}

// networkCmd represents the network command
var networkCmd = &cobra.Command{
	Use:   "network",
	Short: "A brief description of your command",
	Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("请明确调用 create 等命令")
	},
}

func init() {

	networkCmd.AddCommand(createCmd)
	createCmd.Flags().StringVarP(&createDriver, "driver", "d", "bridge", "网络驱动，比如 bridge/macvlan")
	createCmd.Flags().StringVar(&createSubnet, "subnet", "", "Subnet in CIDR format that represents a network segment, 比如 192.168.2.0/24")

	networkCmd.AddCommand(listCmd)
	networkCmd.AddCommand(removeCmd)
	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// networkCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// networkCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

	rootCmd.AddCommand(networkCmd)
}
